//
//  ReportExportViewController.m
//  iExpense
//
//  Created by JackyLiang on 09-7-12.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ReportExportViewController.h"
#import "DatePickerViewController.h"
#import "TypeViewController.h"
#import "CurrencyViewController.h"
#import "ReportByViewController.h"
#import "ReportTableViewController.h"
#import "PieChartViewController.h"


@implementation ReportExportViewController
@synthesize filterTable;
@synthesize filterParametersList;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Report";
	
	filterParametersList = [[NSArray alloc] initWithObjects:@"Begin Date", @"End Date", @"Type",
						   @"Currency", @"Report By",  nil];
	
	// Set the right button Save
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Export" style:UIBarButtonItemStyleBordered
																			  target:self action:@selector(export:)] autorelease];
	
	// Set the right button Save
	self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Table" style:UIBarButtonItemStyleBordered
																			  target:self action:@selector(table:)] autorelease];
	
	// Set up the toolbar items
	UIBarButtonItem *tableButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"table.png"]
																   style:UIBarButtonItemStylePlain target:self action:@selector(table:)];
	
	UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																			   target:self action:@selector(space:)];
	
	UIBarButtonItem *curveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"curve.png"]
																  style:UIBarButtonItemStylePlain target:self action:@selector(curve:)];
	
	UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																				 target:self action:@selector(space:)];
	
	UIBarButtonItem *pieButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"pie.png"]
																	  style:UIBarButtonItemStylePlain target:self action:@selector(pie:)];
	
	UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																				target:self action:@selector(space:)];
	
	UIBarButtonItem *exportButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"export.png"]
																   style:UIBarButtonItemStylePlain target:self action:@selector(export:)];
	
	self.toolbarItems = [NSArray arrayWithObjects:tableButton, leftSpace, curveButton, centerSpace, pieButton, rightSpace, exportButton, nil];
	
	[tableButton release];
	[leftSpace release];
	[curveButton release];
	[centerSpace release];
	[pieButton release];
	[rightSpace release];
	[exportButton release];
}

// Call this function whenever the view will appear
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	
	self.navigationController.toolbarHidden = NO;
}

- (IBAction)export:(id)sender {
	MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init];
	mailCtrl.mailComposeDelegate = self;
	
	NSString *address1 = @"john@gmail.com";
	NSString *address2 = @"jane@yahoo.com";
	[mailCtrl setToRecipients:[NSArray arrayWithObjects:address1, address2, nil]];
	
	[mailCtrl setSubject:@"My Expense from June 1 to June 30"];
	[mailCtrl setMessageBody:@"Here is the expense report." isHTML:NO];
	
	[self presentModalViewController:mailCtrl animated:YES];
	[mailCtrl release];	
}

- (IBAction)table:(id)sender {
	ReportTableViewController *controller = [[ReportTableViewController alloc] initWithNibName:@"ReportTableView" bundle:nil];
	//controller.editingItem = self.editingItem;
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];
}

- (IBAction)curve:(id)sender {
}

- (IBAction)pie:(id)sender {
	PieChartViewController *controller = [[PieChartViewController alloc] initWithNibName:@"PieChartView" bundle:nil];
	//controller.editingItem = self.editingItem;
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];
}

- (IBAction)space:(id)sender {
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
		  didFinishWithResult:(MFMailComposeResult)result
						error:(NSError *)error {
	switch(result) {
		case MFMailComposeResultCancelled:
			[controller dismissModalViewControllerAnimated:YES];
			break;
		case MFMailComposeResultSaved:
			[controller dismissModalViewControllerAnimated:YES];
			break;
		case MFMailComposeResultSent:
			[controller dismissModalViewControllerAnimated:YES];
			break;
		case MFMailComposeResultFailed:
			[controller dismissModalViewControllerAnimated:YES];
			break;
		default:
			[controller dismissModalViewControllerAnimated:YES];
			break;
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.filterParametersList count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	cell.textLabel.text = [filterParametersList objectAtIndex:indexPath.row];
	switch (indexPath.row) {
		case 0:
			cell.detailTextLabel.text = @"June 1, 2009";
			break;
		case 1:
			cell.detailTextLabel.text = @"June 30, 2009";
			break;
		case 2:
			cell.detailTextLabel.text = @"Personal";
			break;
		case 3:
			cell.detailTextLabel.text = @"USD";
			break;
		case 4:
			cell.detailTextLabel.text = @"Category";
			break;
		default:
			break;
	}
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
    return cell;
}

// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
	[filterTable deselectRowAtIndexPath:indexPath animated:NO];
	
	if (indexPath.row == 0 || indexPath.row == 1) {
		DatePickerViewController *controller = [[DatePickerViewController alloc] initWithNibName:@"DatePickerView" bundle:nil];
		//controller.editingItem = self.editingItem;
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	else if (indexPath.row == 2) {
		TypeViewController *controller = [[TypeViewController alloc] initWithNibName:@"TypeView" bundle:nil];
		//controller.editingItem = self.editingItem;
		//controller.row = indexPath.row;
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];		
	}
	else if (indexPath.row == 3) {
		CurrencyViewController *controller = [[CurrencyViewController alloc] initWithNibName:@"CurrencyView" bundle:nil];
		//controller.editingItem = self.editingItem;
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];		
	}
	else if (indexPath.row == 4) {
		ReportByViewController *controller = [[ReportByViewController alloc] initWithNibName:@"ReportByView" bundle:nil];
		//controller.editingItem = self.editingItem;
		//controller.row = indexPath.row;
		[self.navigationController pushViewController:controller animated:YES];
		[controller release];
	}
	else {
		// will never happen here
	}
}


- (void)dealloc {
	[filterTable release];
	[filterParametersList release];
    [super dealloc];
}


@end
